package com.example.fmgidsprototype;

import java.net.URLConnection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;

import com.example.fmgidsprototype.model.Venue;
import com.google.android.gms.maps.model.LatLng;

public class Util {

	private static Util instance;
	
	public static final Map<String, String> COLOR_CODES = createMap();
	public static final Map<String, String> CAT_ICONS = createIconMap();
	public static LatLng lastLocation;
	
	private static Map<String, String> createMap(){
		Map<String, String> temp = new HashMap<String, String>();
		temp.put("cultuur", "#FDD37E");
		temp.put("escapism", "#9ACFA1");
		temp.put("nightlife", "#90C2CD");
		temp.put("winkels", "#B6B493");
		temp.put("bars", "#CDDC51");
		temp.put("eten", "#9591BB");
		temp.put("kids", "#22ABBD");
		return Collections.unmodifiableMap(temp);
	}
	
	private static Map<String, String> createIconMap(){
		Map<String, String> temp = new HashMap<String, String>();
		temp.put("cultuur", "ic_cultuur.png");
		temp.put("escapism", "ic_escapism.png");
		temp.put("nightlife", "ic_nightlife.png");
		temp.put("winkels", "ic_winkels.png");
		temp.put("bars", "ic_bars.png");
		temp.put("eten", "ic_eten.png");
		temp.put("kids", "ic_kids.png");
		return Collections.unmodifiableMap(temp);
	}
	
	public static synchronized Util getInstance(){
		if(instance == null)
			instance = new Util();
		return instance;
	}
	
	private Util(){}
	
	public static List<Venue> sort(List<Venue> input){
		//sort venues by distance from zero
		Collections.sort(input, new Comparator<Venue>(){
			//TODO: implement this with Bart's Famous Distance Calculation Method 
			//for now this uses hard coded distance from Point Zero
			public int compare(Venue v1, Venue v2){			
				return (v1.getDistanceFromZero() <= v2.getDistanceFromZero()) ? -1 : 1;
			}
		});
		return input;
	}
	
	public static boolean mustUpdate(long localTimeStamp, long remoteTimeStamp){
		if(localTimeStamp >= remoteTimeStamp)
			return false;
		else
			return true;
	}
	
	public static void updateTimeStamp(long timeStamp, String resourceName, SharedPreferences prefs){
		long current = prefs.getLong(resourceName, 0L);
		Editor editor = prefs.edit();
		if(current != 0L){
			editor.remove(resourceName);
		}
		editor.putLong(resourceName, timeStamp);
	}
	
	/*
	 * @function getRadiusMyDistanceFromCenter
	 * input LatLng myLatLong
	 * 
	 * calculates the distance in meters from the great market "grote markt"-brussel
	 * */
	public static float getRadiusMyDistanceFromCenter(LatLng myLatLong){

		float[] distancebetweenresult=new float[5];
		//"afstand: " + String.format(Locale.US, "%.2f",results[0]/1000) + " km"
		Location.distanceBetween(myLatLong.latitude, myLatLong.longitude, AppConstants.LL_GREATMARKET.latitude, 
				AppConstants.LL_GREATMARKET.longitude, distancebetweenresult);
		return distancebetweenresult[0];
	}
	
	public static String stripExtension(String s){
		return s.substring(0, s.lastIndexOf('.'));
	}
	
	
}
